public class MySingleLinkedList {
    static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int val){
            this.val = val;
        }
    }
    public ListNode head;
    private int usedSize = 0;
    public int getUsedSize(){
        return usedSize;
    }

    //头插法
    public void addFirst(int data){
        ListNode cur = new ListNode(data);
        if (head == null){
            head = cur;
            usedSize++;
            return;
        }//不知道当head为null时，这样处理是否正确
        cur.next = head;
        head = cur;
        usedSize++;
    }

    //尾插法
    public void addLast(int data){
        if (head == null){
            ListNode newhead = new ListNode(data);
            head = newhead;
            usedSize++;
            return;
        }//不知道当head为null时，这样处理是否正确
        ListNode cur = head;
        while (cur.next != null){
            cur = cur.next;
        }
        ListNode curN = new ListNode(data);
        cur.next = curN;
        usedSize++;
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        if (index > getUsedSize() || index < 0){
            System.out.println("index 位置信息错误");
            return;
        }
        if (index == 0){
            addFirst(data);
            return;
        }
        if (index == getUsedSize()){
            addLast(data);
            return;
        }
        int a = 0;
        ListNode cur = head;
        while (a != index-1){
            cur = cur.next;
            a++;
        }
        ListNode curN = new ListNode(data);
        curN.next = cur.next;
        cur.next = curN;
        usedSize++;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if (head == null){
            return false;
        }
        ListNode cur = head;
        while(cur != null){
            if (cur.val == key){
                return true;
            }cur = cur.next;
        }return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
        if (head == null){
            System.out.println("该链表为空链表");
            return;
        }
        if (head.val ==key){
            head = head.next;
            usedSize--;
            return;
        }
        ListNode cur = head;
        while(cur.next != null){
            if (cur.next.val == key){
                cur.next = cur.next.next;
                usedSize--;
                return;
            }cur = cur.next;
        }
        System.out.println("在该链表中找不到关键字key");
    }

    //删除所有值为key的节点
    public void removeAllKey(int key){
        int a = getUsedSize();
        if (head == null){
            System.out.println("该链表为空链表");
            return;
        }
        //如果要将head的判定写在前的话就这样写
        //while(head.val == val){
        //          head = head.next;
        //            if(head == null){
        //                return head;
        //            }
        //        }
        ListNode cur = head;
        while(cur.next != null){
            if (cur.next.val == key){
                cur.next = cur.next.next;
                usedSize--;
            }cur = cur.next;
        }
        if (getUsedSize() == a){
            System.out.println("在该链表中找不到关键字key");
        }
        if (head.val ==key){
            head = head.next;
            usedSize--;
        }
    }

    //得到单链表的长度
    public int size(){
        ListNode cur = head;
        int size = 0;
        while(cur != null){
            size++;
            cur = cur.next;
        }return size;
    }

    public void clear() {
        ListNode cur = head;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = null;
            cur = curN;
        }head = null;
    }

    public void display() {
        if (head == null){
            System.out.println("该链表为空链表");
            return;
        }
        ListNode cur = head;
        while (cur != null){
            System.out.print(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }

    public boolean check(){
        ListNode fast = head;
        ListNode slow = head;
        while(fast !=null){
            fast = fast.next.next;
            slow = slow.next;
        }
        ListNode cur = slow.next;
        while(cur != null){
            ListNode curN = cur.next;
            cur.next = slow;
            slow = cur;
            cur = curN;
        }
        while(head != slow){
            if (head.val != slow.val){
                return false;
            }
            if (head.next == slow){
                head = slow;
            }
            head = head.next;
            slow = slow.next;
        }return true;
    }
}

